package com.hl.card.modular.pack.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hl.card.common.persistence.dao.UserMapper;
import com.hl.card.common.persistence.model.User;
import com.hl.card.common.util.*;
import com.hl.card.modular.pack.dao.DisMemberApplyMapper;
import com.hl.card.modular.pack.dao.DisMemberInfoMapper;
import com.hl.card.modular.pack.dao.DisMemberProcessMapper;
import com.hl.card.modular.pack.model.Coin;
import com.hl.card.modular.pack.model.DisMemberApplyModel;
import com.hl.card.modular.pack.model.DisMemberInfoModel;
import com.hl.card.modular.pack.model.PayVo;
import com.hl.card.modular.pack.service.DisMemberApplyService;
import com.hl.card.modular.pack.service.RewardByCount;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import com.hl.card.common.mq.Mq4Base;
/**
 * 合伙人申请服务层
 * 
 * @author 袁伟
 *
 */
@Service
public class DisMemberApplyServiceImpl implements DisMemberApplyService {
	@Resource(name = "disMemberApplyYMapper")
	private DisMemberApplyMapper disMemberApplyMapper;
	@Autowired
	private RewardByCount rewardByCount;
	@Autowired
	private PayUtils payUtils;
	@Autowired
	private Mq4Base mq4Base;
	@Autowired
	private ResponseData responseData;
	public static String REDISKEY = "redis_key:stock:rob";
	@Value("${dismemberapply.ip}")
	private String return_ip;

	@Autowired
	private StockService stockService;
	@Autowired
	private DisMemberProcessMapper disMemberProcessMapper;

	@Autowired
	private ShortMessageUtil shortMessageUtil;
	@Autowired
	@Resource(name = "disMemberInfoYMapper")
	private DisMemberInfoMapper disMemberInfoMapper;
	@Resource
	private RedisTemplate<String, Object> redisTemplate;
	@Autowired
	private UserMapper userMapper;
	@Value("${pay.config.payUrl}")
	private String payUrl;

	// 深圳航旅，对应支付后台管理里中的商户ID（注意，这里不是微信支付里的商户ID，而是我们的自定义的商户，比如比比旅行需要接入支付中心，我们就需要为比比旅行创建一个商户）
	@Value("${dismemberapply.pay.mchId}")
	private String mchId;

	/**
	 * 合伙人申请
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public ResponseData addDisMemberApply(DisMemberApplyModel disMemberApply) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user_id", disMemberApply.getUser_id());
		map.put("type", disMemberApply.getType());
		int count = disMemberApplyMapper.queryIsExistDisMemberApply(map);
		if (count > 0) {
			responseData.setStatus("0");
			responseData.setData(null);
			responseData.setMsg("请勿重复申请！");
			return responseData;
		}
		if (StringUtils.isNotEmpty(disMemberApply.getRecommendation_no())) {
			if (disMemberApply.getType().equals(0)) { // 矿机
				int reg_count = userMapper.queryRegCodeCount(disMemberApply
						.getRecommendation_no());
				if (reg_count == 0) {
					responseData.setStatus("0");
					responseData.setData(null);
					responseData.setMsg("推荐码不正确！");
					return responseData;
				} else {
					User param = new User();
					param.setUserId(disMemberApply.getUser_id());
					User user = userMapper.selectOne(param);
					if (disMemberApply.getRecommendation_no().equalsIgnoreCase(
							user.getReg_code())) // 如果推荐码填写自己的注册码
					{
						responseData.setStatus("0");
						responseData.setData(null);
						responseData.setMsg("推荐码不正确！");
						return responseData;
					}
					if(!StringUtils.isEmpty(user.getRefer_reg_code()))  //如果用户有上级
					{
						if (!disMemberApply.getRecommendation_no().equalsIgnoreCase(user.getRefer_reg_code())) // 如果用户填写的不是用户上级推荐码
						{
							responseData.setStatus("0");
							responseData.setData(null);
							responseData.setMsg("推荐码不正确！");
							return responseData;
						}
					}
				}
			} else // 商城
			{
				User param = new User();
				param.setUserId(disMemberApply.getUser_id());
				User user = userMapper.selectOne(param);
				if (user.getRefer_reg_code() == null
						|| "".equals(user.getRefer_reg_code())) {
					int reg_count = userMapper.queryRegCodeCount(disMemberApply
							.getRecommendation_no());
					if (reg_count == 0) {
						responseData.setStatus("0");
						responseData.setData(null);
						responseData.setMsg("推荐码不正确！");
						return responseData;
					} else {
						if (disMemberApply.getRecommendation_no()
								.equalsIgnoreCase(user.getReg_code())) // 如果推荐码填写自己的注册码
						{
							responseData.setStatus("0");
							responseData.setData(null);
							responseData.setMsg("推荐码不正确！");
							return responseData;
						}
					}
				} else {
					if (disMemberApply.getRecommendation_no().equalsIgnoreCase(
							user.getReg_code())) // 如果推荐码填写自己的注册码
					{
						responseData.setStatus("0");
						responseData.setData(null);
						responseData.setMsg("推荐码不正确！");
						return responseData;
					}

					if (!user.getRefer_reg_code().equalsIgnoreCase(
							disMemberApply.getRecommendation_no())) // 如果推荐码不是填写的上级推荐码
					{
						responseData.setStatus("0");
						responseData.setData(null);
						responseData.setMsg("推荐码不正确！");
						return responseData;
					}
				}
			}
		}
		disMemberApply.setOrder_no(creatOrderNo());
		disMemberApply.setStatus(0); // 待审核
		disMemberApply.setPay_status(0);
		disMemberApply.setApply_time(DateUtils.getNow());
		double pay_money = 9990;  
		if (disMemberApply.getType().equals(0)) // 矿机
		{
			pay_money = pay_money * disMemberApply.getRob_num();
		}else if(disMemberApply.getType().equals(2))  //980入驻商城
		{
			pay_money=998;
		}else if(disMemberApply.getType().equals(3))
		{
			pay_money=3650;
		}else if(disMemberApply.getType().equals(4))
		{
			pay_money=9990;
		}
		disMemberApply.setPay_money(pay_money);
		BigDecimal payable_amount = new BigDecimal(pay_money).setScale(0);
		disMemberApply.setPayable_amount(Integer.parseInt(payable_amount + ""));
		int result = disMemberApplyMapper.addDisMemberApply(disMemberApply);
		if (result == 0) {
			responseData.setStatus("0");
			responseData.setData(null);
			responseData.setMsg("添加失败");
		} else {
			JSONObject o = new JSONObject();
			o.put("orderNo", disMemberApply.getOrder_no());
			mq4Base.send("queue.delay.status", o.toJSONString(), 60*60*1000);
			responseData.setData(disMemberApply.getOrder_no());
			responseData.setStatus("1");
			responseData.setMsg("添加成功");
		}
		return responseData;
	}

	public ResponseData queryDisMemberInfoCode(String code) {
		int count = userMapper.queryRegCodeCount(code);
		if (count == 0) {
			responseData.setStatus("0");
			responseData.setData("false");
			responseData.setMsg("推荐码不正确！");
			return responseData;
		} else {
			responseData.setStatus("1");
			responseData.setData("true");
			responseData.setMsg("操作成功！");
		}
		return responseData;
	}

	/**
	 * 合伙人申请支付
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public ResponseData payDisMemberApply(String user_id, Integer payType,
			String ip, String openId, String channelId, Integer type,
			String backUrl, String shopRedirectUrl,String returnUrl) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user_id", user_id);
		map.put("type", type);
		DisMemberApplyModel disMemberApply = disMemberApplyMapper
				.searchDisMemberApplyByUserId(map);
		if (disMemberApply == null) {
			responseData.setMsg("申请单不存在！");
			responseData.setData(null);
			responseData.setStatus("0");
			return responseData;
		}
		if (payType.equals(3) || payType.equals(4)) // 线下支付
		{
			disMemberApply.setPay_status(2);
			disMemberApply.setStatus(0);
			disMemberApply.setPay_type(payType);
			disMemberApplyMapper.updateDisMemberApply(disMemberApply);
			responseData.setMsg("操作成功！");
			responseData.setData(null);
			responseData.setStatus("1");
			return responseData;
		}
		String cpCode = "";
		if (disMemberApply.getType().equals(0)) // 矿机
		{
			cpCode = "TJHHR";
		}else if(disMemberApply.getType().equals(2))
		{
			cpCode = "SCDPRZJL";
		} 
		else if(disMemberApply.getType().equals(3))
		{
			cpCode = "BXJHJL";
		} 
		else if(disMemberApply.getType().equals(4))
		{
			cpCode = "PPRZJL";
		} 
		else {
			cpCode = "RZSC";
		}
		// /////////////////////////////////线上支付/////////////////////////////////////////
		if (disMemberApply.getPay_status().equals(0)
				|| disMemberApply.getPay_status().equals(1)) {
			String url = payUrl + "/requestSelectPay?";
			PayVo payVo = new PayVo();
			payVo.setMchId(mchId);
			payVo.setUserId(user_id);
			payVo.setMchOrderNo(disMemberApply.getOrder_no() + "");
			DecimalFormat df = new DecimalFormat("0");
			payVo.setAmount(df.format(disMemberApply.getPay_money() * 100));// 单位：分
			payVo.setDisPlayAmount(df.format(disMemberApply.getPay_money()));// 单位：分

			payVo.setParam1("{\"code\":\"" + cpCode + "\",\"begin_time\":\""
					+ disMemberApply.getApply_time() + "\",\"user_id\":\""
					+ user_id + "\"}");
			payVo.setCurrency("cny");
			payVo.setBackUrl(backUrl);
			payVo.setDevice("WEB");
			payVo.setSubject("商铺合伙人购买支付");
			payVo.setBody("商铺合伙人购买支付");
			payVo.setOpenId(openId);
			payVo.setClientIp(ip);
			payVo.setNotifyUrl(return_ip + "/api/disMemberApply/payResult.do");
			payVo.setExtra("{\n" + "  \"productId\": \""
					+ disMemberApply.getOrder_no()
					+ "\",\n"
					+ "  \"openId\": \""
					+ openId
					+ "\",\n"
					+ // 注意：测试WX_JSAPI时需要预先通过其他途径获取到用户openID，这里填写的是我自己的
					"  \"sceneInfo\": {\n" + "    \"h5_info\": {\n"
					+ "      \"type\": \"Wap\",\n"
					+ "      \"wap_url\": \"http://localhost.com\",\n"
					+ "      \"wap_name\": \"商铺合伙人购买支付\"\n" + "    }\n"
					+ "  }\n" + " ,\"discountable_amount\":\"0.00\"," + // 面对面支付扫码参数：可打折金额
																		// 可打折金额+不可打折金额=总金额
					"  \"undiscountable_amount\":\"0.00\"," + // 面对面支付扫码参数：不可打折金额
					"}");
			payVo.setRedirectUrl(shopRedirectUrl + "?orderNo=" + payVo.getMchOrderNo() + "&mchId=" + mchId);
			payVo.setReturnUrl(returnUrl);
			payVo.setAsyn("0");
			String param = JSON.toJSONString(payVo);
			Map<String, String> params = new HashMap<>();
			params.put("params", param);
			try {
				String s = HttpURLClient.post(params, url);
				responseData = JSON.parseObject(s, ResponseData.class);
				return responseData;
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;

		} else {
			responseData.setMsg("该申请单已经支付完成,请勿重复支付！");
			responseData.setStatus("0");
			responseData.setData(null);
		}
		return responseData;
	}

	/**
	 * 生成订单编号
	 * 
	 * @return
	 */
	public String creatOrderNo() {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		int a = (int) (Math.random() * 900) + 100;
		String orderNo = format.format(new Date()) + a;
		return "D" + orderNo;
	}

	/**
	 * 生成合伙人编号
	 * 
	 * @return
	 */
	// public String creatID() {
	// redisTemplate.setValueSerializer(new StringRedisSerializer());
	// boolean is_exists=redisTemplate.hasKey("dismember_id");
	// long count=100001;
	// if(is_exists)
	// {
	// count=Long.parseLong(redisTemplate.opsForValue().get("dismember_id")+"");
	// if(count>=200000)
	// {
	// return "";
	// }
	// }else
	// {
	// redisTemplate.opsForValue().set("dismember_id", "100001");
	// }
	// redisTemplate.opsForValue().increment("dismember_id", 1);
	// String format_num = String.format("%06d",count);
	// return "WK" + format_num;
	// }
	/**
	 * 支付异步通知
	 * 
	 * @param map
	 * @return
	 */
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public boolean payResult(Map<String, Object> map) {
		boolean is_result = false;
		if ("2".equals(map.get("status"))) // 支付成功
		{
			DisMemberApplyModel disMemberApply = disMemberApplyMapper
					.searchDisMemberApplyByNo(map.get("mchOrderNo").toString());
			if (!disMemberApply.getPay_status().equals(2)) {
				disMemberApply.setFlow_no(map.get("channelOrderNo").toString());
				Long s = new Long(map.get("paySuccTime").toString());
				Date d = new Date(s);
				disMemberApply.setPay_time(d);
				int pay_type = 0;
				if (map.get("channelId").equals("ALIPAY_WAP")) {
					pay_type = 0;
				} else if (map.get("channelId").equals("WX_MWEB")) {
					pay_type = 1;
				} else if (map.get("channelId").equals("WX_JSAPI")) {
					pay_type = 2;
				} else {
					pay_type = 3;
				}
				disMemberApply.setPay_type(pay_type);
				disMemberApply.setPay_status(2);
				disMemberApply.setStatus(2);
				disMemberApply.setRec_platform(map.get("channelAccount") + "");
				disMemberApplyMapper.updateDisMemberApply(disMemberApply);

				Date time = DateUtils.getNow(); // 合伙人帐号开通时间
				User param = new User();
				param.setUserId(disMemberApply.getUser_id()); // 根据用户ID查找用户信息
				User user = userMapper.selectOne(param);
				DisMemberInfoModel info = disMemberInfoMapper
						.queryDisMemberInfoById(disMemberApply.getUser_id());
				if (info == null) {
					DisMemberInfoModel disMemberInfo = new DisMemberInfoModel();
					disMemberInfo.setDis_user_id(disMemberApply.getUser_id());
					disMemberInfo.setDis_user_name(disMemberApply
							.getUser_name());
					disMemberInfo.setBinding_phone(disMemberApply
							.getBinding_phone());
					disMemberInfo.setIdno(disMemberApply.getId_no());
					disMemberInfo.setJurisdiction("1,2,3");
					disMemberInfo.setDis_member_time(time);
					disMemberInfo.setDis_recommendation_no(user.getReg_code());
					disMemberInfo.setAdd_time(DateUtils.DateToStr(time,
							"yyyy-MM-dd HH:mm:ss"));
					disMemberInfo.setUpdate_time(DateUtils.DateToStr(time,
							"yyyy-MM-dd HH:mm:ss"));
					disMemberInfo.setStatus(0);
					disMemberInfo.setType("1");
					disMemberInfo.setDis_recomm_pcode(disMemberApply
							.getRecommendation_no()); // 上级合伙人推荐码
					if (disMemberApply.getType().equals(0)) {
						disMemberInfo.setRob_num(disMemberApply.getRob_num());
					}else if(disMemberApply.getType().equals(2))
					{
						disMemberInfo.setCell_num(1);
					}
					else if(disMemberApply.getType().equals(3))
					{
						disMemberInfo.setWhite_num(1);
					}
					else if(disMemberApply.getType().equals(4))
					{
						disMemberInfo.setStar_num(1);
					}
					else {
						disMemberInfo.setShop_num(1);
					}
					disMemberInfoMapper.addMemberInfo(disMemberInfo); // 开通合伙人权限
					if(StringUtils.isEmpty(user.getRefer_reg_code()))
					{
						Map<String, Object> param2 = new HashMap<String, Object>();
						param2.put("user_id", user.getUserId());
						param2.put("refer_reg_code", disMemberApply.getRecommendation_no()); 
						userMapper.updateReferRegCode(param2);
					}
					
				} else {
					if (disMemberApply.getType().equals(0)) {
						int total_num = info.getRob_num() == null ? 0 : info
								.getRob_num();
						total_num += disMemberApply.getRob_num().intValue();
						info.setRob_num(total_num);
						disMemberInfoMapper.updateMemberNum(info);

					}else if(disMemberApply.getType().equals(2))
					{
						int total_num = info.getCell_num() == null ? 0 : info
								.getCell_num();
						total_num += 1;
						info.setCell_num(total_num);
						disMemberInfoMapper.updateCellMemberNum(info);
					}else if(disMemberApply.getType().equals(3))
					{
						int total_num = info.getWhite_num() == null ? 0 : info
								.getWhite_num();
						total_num += 1;
						info.setWhite_num(total_num);
						disMemberInfoMapper.updateWhiteMemberNum(info);
					}
					else if(disMemberApply.getType().equals(4))
					{
						int total_num = info.getStar_num() == null ? 0 : info
								.getStar_num();
						total_num += 1;
						info.setStar_num(total_num);
						disMemberInfoMapper.updateStarMemberNum(info);
					}
					else {
						int total_num = info.getShop_num() == null ? 0 : info
								.getShop_num();
						total_num += 1;
						info.setShop_num(total_num);
						disMemberInfoMapper.updateShopMemberNum(info);

					}

				}
				if (disMemberApply.getType().equals(0)) // 矿机减库存
				{
					stockService.stock(REDISKEY, disMemberApply.getRob_num());
				}else if(disMemberApply.getType().equals(2))  //998调用奖励
				{
					
					JSONObject o = new JSONObject();
					o.put("function", "makeAward");
					o.put("parentCode", "SCDPRZJL");
					o.put("userId", user.getUserId());
					o.put("amount", "360");
					o.put("orderId", disMemberApply.getOrder_no());
					o.put("queueName", "queue.award.api");
					mq4Base.send("queue.award",o.toJSONString());
				}
				else if(disMemberApply.getType().equals(3))  //百星调用奖励
				{
					
					JSONObject o = new JSONObject();
					o.put("function", "makeAward");
					o.put("parentCode", "BXJHJL");
					o.put("userId", user.getUserId());
					o.put("amount", "360");
					o.put("orderId", disMemberApply.getOrder_no());
					o.put("queueName", "queue.award.api");
					mq4Base.send("queue.award",o.toJSONString());
				}else if(disMemberApply.getType().equals(4))  //品牌调用奖励
				{
					
					JSONObject o = new JSONObject();
					o.put("function", "makeAward");
					o.put("parentCode", "PPRZJL");
					o.put("userId", user.getUserId());
					o.put("amount", "360");
					o.put("orderId", disMemberApply.getOrder_no());
					o.put("queueName", "queue.award.api");
					mq4Base.send("queue.award",o.toJSONString());
				}
				else {
					Object res = rewardByCount.doBatchReward(user.getUserId(),
							"RZSC", "0", 12, disMemberApply.getOrder_no());
					System.out.println(res); // 商铺手动调用奖励
				}
				is_result = true;
			}
		}
		return is_result;
	}

	/**
	 * 生成合伙人推荐码
	 * 
	 * @return
	 */
	// public String creatRecommendationNo()
	// {
	// char[] arr = {48,49,50,51,52,53,54,55,56,57,//从0到9的数字
	// 65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90//从A到Z的数字
	// };
	// int i=1;
	// String recommendation_no="";
	// while(i++<=6){ //循环六次，得到六位数的验证码
	// char msg =arr[(int)(Math.random()*36)];
	// recommendation_no+=msg;
	// }
	// int count=disMemberInfoMapper.queryDisMemberInfoCode(recommendation_no);
	// if(count>0)
	// {
	// return creatRecommendationNo();
	// }
	// return recommendation_no;
	// }

	/**
	 * 合伙人详情
	 * 
	 * @param user_id
	 * @return
	 */
	public ResponseData searchDisMemberApplyByUserId(String user_id,
			Integer type) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user_id", user_id);
		map.put("type", type);
		DisMemberApplyModel disMemberApplyModel = disMemberApplyMapper
				.searchDisMemberApplyByUserId(map);
		if(disMemberApplyModel!=null)
		{
			disMemberApplyModel.setTime(DateUtils.getNow());
		}
		responseData.setData(disMemberApplyModel);
		responseData.setMsg("操作成功！");
		responseData.setStatus("1");
		return responseData;
	}

	/**
	 * 查询订单是否存在
	 * 
	 * @param user_id
	 * @return
	 */
	@Override
	public ResponseData queryIsExistDisMemberApply(String user_id, Integer type) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user_id", user_id);
		map.put("type", type);
		responseData = new ResponseData();
		DisMemberApplyModel disMemberApply = disMemberApplyMapper
				.searchDisMemberApplyByUserId(map);
		if (disMemberApply == null) {
			responseData.setData("none");
			responseData.setMsg("操作成功！");
			responseData.setStatus("1");
		} else if (disMemberApply.getPay_status().equals(0)
				|| disMemberApply.getPay_status().equals(1)) {
			responseData.setData("wait_pay");
			responseData.setMsg("操作成功！");
			responseData.setStatus("1");
		} else if (disMemberApply.getPay_status().equals(2)) {
			responseData.setData("complete_pay");
			responseData.setMsg("操作成功！");
			responseData.setStatus("1");
		}
		return responseData;
	}

	/**
	 * 我的商铺/矿机
	 */
	@Override
	public ResponseData queryMemberApplyList(String user_id, Integer type,
			int currentPage, int pageSize) {
		Integer start = pageSize * (currentPage - 1);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user_id", user_id);
		map.put("type", type);
		map.put("start", start);
		map.put("size", pageSize); 
		List<DisMemberApplyModel> list = disMemberApplyMapper
				.queryMemberApplyList(map);
		responseData.setData(list);
		responseData.setMsg("操作成功");
		responseData.setStatus("1");
		return responseData;

	}

	/**
	 * 签约资料
	 */
	public ResponseData queryDisMemberApply(String user_id) {
		DisMemberApplyModel disMemberApply = disMemberApplyMapper
				.searchDisMemberApplyInfo(user_id);
		if (disMemberApply == null) {
			responseData.setData(null);
			responseData.setMsg("申请单未完成支付！");
			responseData.setStatus("1");
			return responseData;
		}
		responseData.setData(disMemberApply);
		responseData.setMsg("操作成功！");
		responseData.setStatus("1");
		return responseData;
	}

	/**
	 * 取消订单
	 * 
	 * @param user_id
	 * @return
	 */
	@Override
	public ResponseData cancelDisMemberApply(String user_id,Integer type) {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("user_id",user_id);
		map.put("type",type);
		disMemberApplyMapper.cancelDisMemberApply(map);
		responseData.setData(null);
		responseData.setMsg("操作成功！");
		responseData.setStatus("1");
		return responseData;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public void addShop(DisMemberApplyModel disMemberApply, User user) {
		// 添加合伙人申请列表
		disMemberApplyMapper.addDisMemberApply(disMemberApply);
		DisMemberInfoModel model = disMemberInfoMapper
				.queryDisMemberInfoByPhone(disMemberApply.getBinding_phone());
		if (model == null) {
			String dis_recomm_pcode = disMemberApply.getRecommendation_no();
			// 添加合伙人列表
			DisMemberInfoModel disMemberInfo = new DisMemberInfoModel();
			disMemberInfo.setDis_user_name(disMemberApply.getUser_name());
			disMemberInfo.setDis_recommendation_no(user.getReg_code());
			disMemberInfo.setStatus(0);
			disMemberInfo.setShop_num(1);
			disMemberInfo.setAdd_time(DateUtils.getNow("yyyy-MM-dd HH:mm:ss"));
			disMemberInfo.setIdno(disMemberApply.getId_no());
			disMemberInfo.setBinding_phone(disMemberApply.getBinding_phone());
			disMemberInfo.setDis_recomm_pcode(dis_recomm_pcode);
			disMemberInfo.setBinding_email(user.getEmail());
			disMemberInfo.setDis_user_id(user.getUserId());
			disMemberInfoMapper.addMemberInfo(disMemberInfo);
			if(StringUtils.isEmpty(user.getRefer_reg_code()))
			{
				Map<String, Object> param2 = new HashMap<String, Object>();
				param2.put("user_id", user.getUserId());
				param2.put("refer_reg_code", disMemberApply.getRecommendation_no());
				userMapper.updateReferRegCode(param2);
			}
		} else {
			int total_num = model.getShop_num() == null ? 0 : model
					.getShop_num();
			total_num += 1;
			model.setShop_num(total_num);
			disMemberInfoMapper.updateShopMemberNum(model);
		}
	}
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public void addSinShop(DisMemberApplyModel disMemberApply, User user) {
		// 添加合伙人申请列表
		disMemberApplyMapper.addDisMemberApply(disMemberApply);
		DisMemberInfoModel model = disMemberInfoMapper
				.queryDisMemberInfoByPhone(disMemberApply.getBinding_phone());
		if (model == null) {
			String dis_recomm_pcode = disMemberApply.getRecommendation_no();
			// 添加合伙人列表
			DisMemberInfoModel disMemberInfo = new DisMemberInfoModel();
			disMemberInfo.setDis_user_name(disMemberApply.getUser_name());
			disMemberInfo.setDis_recommendation_no(user.getReg_code());
			disMemberInfo.setStatus(0);
			disMemberInfo.setCell_num(1);
			disMemberInfo.setAdd_time(DateUtils.getNow("yyyy-MM-dd HH:mm:ss"));
			disMemberInfo.setIdno(disMemberApply.getId_no());
			disMemberInfo.setBinding_phone(disMemberApply.getBinding_phone());
			disMemberInfo.setDis_recomm_pcode(dis_recomm_pcode);
			disMemberInfo.setBinding_email(user.getEmail());
			disMemberInfo.setDis_user_id(user.getUserId());
			disMemberInfoMapper.addMemberInfo(disMemberInfo);
			if(StringUtils.isEmpty(user.getRefer_reg_code()))
			{
				Map<String, Object> param2 = new HashMap<String, Object>();
				param2.put("user_id", user.getUserId());
				param2.put("refer_reg_code", disMemberApply.getRecommendation_no());
				userMapper.updateReferRegCode(param2);
			}
		} else {
			int total_num = model.getCell_num() == null ? 0 : model
					.getCell_num();
			total_num += 1;
			model.setCell_num(total_num);
			disMemberInfoMapper.updateCellMemberNum(model);
		}
	}
	
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public void addWhiteShop(DisMemberApplyModel disMemberApply, User user) {
		// 添加合伙人申请列表
		disMemberApplyMapper.addDisMemberApply(disMemberApply);
		DisMemberInfoModel model = disMemberInfoMapper
				.queryDisMemberInfoByPhone(disMemberApply.getBinding_phone());
		if (model == null) {
			String dis_recomm_pcode = disMemberApply.getRecommendation_no();
			// 添加合伙人列表
			DisMemberInfoModel disMemberInfo = new DisMemberInfoModel();
			disMemberInfo.setDis_user_name(disMemberApply.getUser_name());
			disMemberInfo.setDis_recommendation_no(user.getReg_code());
			disMemberInfo.setStatus(0);
			disMemberInfo.setWhite_num(1);
			disMemberInfo.setAdd_time(DateUtils.getNow("yyyy-MM-dd HH:mm:ss"));
			disMemberInfo.setIdno(disMemberApply.getId_no());
			disMemberInfo.setBinding_phone(disMemberApply.getBinding_phone());
			disMemberInfo.setDis_recomm_pcode(dis_recomm_pcode);
			disMemberInfo.setBinding_email(user.getEmail());
			disMemberInfo.setDis_user_id(user.getUserId());
			disMemberInfoMapper.addMemberInfo(disMemberInfo);
			if(StringUtils.isEmpty(user.getRefer_reg_code()))
			{
				Map<String, Object> param2 = new HashMap<String, Object>();
				param2.put("user_id", user.getUserId());
				param2.put("refer_reg_code", disMemberApply.getRecommendation_no());
				userMapper.updateReferRegCode(param2);
			}
		} else {
			int total_num = model.getWhite_num() == null ? 0 : model
					.getWhite_num();
			total_num += 1;
			model.setWhite_num(total_num);
			disMemberInfoMapper.updateWhiteMemberNum(model);
		}
	}
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public void addStarShop(DisMemberApplyModel disMemberApply, User user) {
		// 添加合伙人申请列表
		disMemberApplyMapper.addDisMemberApply(disMemberApply);
		DisMemberInfoModel model = disMemberInfoMapper
				.queryDisMemberInfoByPhone(disMemberApply.getBinding_phone());
		if (model == null) {
			String dis_recomm_pcode = disMemberApply.getRecommendation_no();
			// 添加合伙人列表
			DisMemberInfoModel disMemberInfo = new DisMemberInfoModel();
			disMemberInfo.setDis_user_name(disMemberApply.getUser_name());
			disMemberInfo.setDis_recommendation_no(user.getReg_code());
			disMemberInfo.setStatus(0);
			disMemberInfo.setStar_num(1);
			disMemberInfo.setAdd_time(DateUtils.getNow("yyyy-MM-dd HH:mm:ss"));
			disMemberInfo.setIdno(disMemberApply.getId_no());
			disMemberInfo.setBinding_phone(disMemberApply.getBinding_phone());
			disMemberInfo.setDis_recomm_pcode(dis_recomm_pcode);
			disMemberInfo.setBinding_email(user.getEmail());
			disMemberInfo.setDis_user_id(user.getUserId());
			disMemberInfoMapper.addMemberInfo(disMemberInfo);
			if(StringUtils.isEmpty(user.getRefer_reg_code()))
			{
				Map<String, Object> param2 = new HashMap<String, Object>();
				param2.put("user_id", user.getUserId());
				param2.put("refer_reg_code", disMemberApply.getRecommendation_no());
				userMapper.updateReferRegCode(param2);
			}
		} else {
			int total_num = model.getStar_num() == null ? 0 : model
					.getStar_num();
			total_num += 1;
			model.setStar_num(total_num);
			disMemberInfoMapper.updateStarMemberNum(model);
		}
	}
}
